package com.github.binarywang.demo.wechat.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hhx
 */
@RestController
public class SchoolController {

    private static Integer schoolCount = 0;

    private static Integer fireCount = 0;

    private static Integer shareSchoolCount = 0;

    private static Integer shareFireCount = 0;

    @GetMapping("/visit")
    public ResponseEntity<Map<String,Integer>> school(){
        Map<String,Integer> map = new HashMap<>(2);
        map.put("西安学院访问量",schoolCount);
        map.put("消防访问量",fireCount);
        map.put("西安学院分享统计",shareSchoolCount);
        map.put("消防分享统计",shareFireCount);
        return ResponseEntity.ok(map);
    }

    private static final String FIRE = "fire";
    private static final String SCHOOL = "school";

    @PostMapping("/share/{page}")
    public ResponseEntity<String> share(@PathVariable String page){
        if(FIRE.equals(page)){
            System.out.println("share page fire success , count : ------------------------," + ++ shareFireCount);
        }else if(SCHOOL.equals(page)){
            System.out.println("share page fire success , count : ------------------------," + ++ shareFireCount);
        }else {
            System.out.println("error : unknown page");
        }
        return ResponseEntity.ok("success");
    }

    @GetMapping("/school")
    public ModelAndView school(Model model){
        System.out.println("visit school ------------------------," + ++ schoolCount);
        return new ModelAndView("school","model",model);
    }

    @GetMapping("/fire")
    public ModelAndView fire(Model model){
        System.out.println("visit fire ------------------------," + ++ fireCount);
        return new ModelAndView("fire","model",model);
    }

}
